//京东云仓
import { CommonTable } from "@/pages/OrderModule/common/components/CommonTable";
import SubTitle from "@/pages/OrderModule/common/components/SubTitle";
import { Alert, Popover, Space, Switch, message } from "antd";
import { useState, useEffect } from "react";
import { queryJdStoreWarehouse, updateJdStoreWarehouse } from "../../service";
import { QuestionCircleOutlined } from "@ant-design/icons";

const JdCloudWarehouse = ({ tab }) => {
	const [tableData, setTableData] = useState([]); //表格数据

	useEffect(() => {
		if (tab == 1) {
			getEcStoreListPageOp();
		}
	}, [tab]);

	const titleRender = text => {
		return (
			<div>
				<span>{`${text}仓订单是否保留`}</span>
				<Popover
					content={
						<span style={{ display: "block", width: "186px" }}>
							1、关闭：则订单“自动作废”
							<br />
							2、开启：则订单同步进来会保留
						</span>
					}
					title={null}
				>
					<QuestionCircleOutlined style={{ color: "#999", marginLeft: "8px" }} />
				</Popover>
			</div>
		);
	};

	//查询店铺表格数据
	const getEcStoreListPageOp = async () => {
		window.START_LOADING()
		const { success, data = [] } = await queryJdStoreWarehouse({
			// current: 1,
			// size: 500
		});
		if (success) {
			setTableData(data);
		}
		window.END_LOADING();
	};

	//修改订单基础配置数据
	const updateMergeOrderStatus = async (val, record, code) => {
		window.START_LOADING()
		const { success, msg = "" } = await updateJdStoreWarehouse({
			// id: record.id,
			...record,
			[code]: val ? 1 : 0
		});
		if (success) {
			message.success(msg);
			getEcStoreListPageOp();
		}
		window.END_LOADING();
	};

	const renderAlertMsg = () => {
		return (
			<div>
				<div>1、只有京东店铺才可以配置京仓与云仓的配置；</div>
				<div>2、关闭后会自动作废，开启后不会自动作废，新店铺默认是关闭的；</div>
				<div>3、修改配置后，只对新订单有影响，针对旧订单不起作用；</div>
				{/* <div>4、清单模板开启后，该店铺的云仓与京仓标记的订单打印的快递模板都是固定；</div> */}
				<div>4、禁止物流回传开启后，该店铺的云仓与京仓标记的订单不会发起物流回传任务；</div>
				<div>5、开启后，京仓云仓订单，和正常订单一样匹配商品、审核付款，并且不会因为“平台已发货”而自动作废或进入”发“异常。</div>
			</div>
		);
	};

	const tableColumns = [
		{
			title: "序号",
			dataIndex: "index",
			key: "index",
			width: 60,
			render: (t, r, i) => i + 1
		},
		{
			title: "京东店铺",
			dataIndex: "storeName"
		},
		{
			title: "授权状态",
			dataIndex: "field4",
			render: (_, record) => {
				const nowTime = Date.now();
				//判断店铺是否过期，true: 没过期
				const isExprie = nowTime < record.authExpireTime * 1000;
				if (isExprie) {
					return "已授权";
				} else {
					return "授权过期";
				}
			}
		},
		{
			title: titleRender("云"),
			dataIndex: "enableCloudWarehouse",
			render: (_, record) => {
				return (
					<Switch
						checkedChildren="已开启"
						unCheckedChildren="已关闭"
						checked={record.enableCloudWarehouse}
						onChange={val => {
							updateMergeOrderStatus(val, record, "enableCloudWarehouse");
						}}
					/>
				);
			}
		},
		{
			title: titleRender("京"),
			dataIndex: "enableJingWarehouse",
			render: (_, record) => {
				return (
					<Switch
						checkedChildren="已开启"
						unCheckedChildren="已关闭"
						checked={record.enableJingWarehouse}
						onChange={val => {
							updateMergeOrderStatus(val, record, "enableJingWarehouse");
						}}
					/>
				);
			}
		},
		{
			title: "禁止物流回传",
			dataIndex: "forbidLogisticsReturn",
			render: (_, record) => {
				return (
					<Switch
						checkedChildren="已开启"
						unCheckedChildren="已关闭"
						checked={record.forbidLogisticsReturn}
						onChange={val => {
							updateMergeOrderStatus(val, record, "forbidLogisticsReturn");
						}}
					/>
				);
			}
		}
	];

	return (
		<Space direction="vertical" size={20}>
			<SubTitle title="信息详情" as="s2" />
			<Alert message={renderAlertMsg()} type="info" showIcon />
			<CommonTable columns={tableColumns} dataSource={tableData} pagination={false} scroll={{ y: 200 }} rowKey={"storeId"} />
		</Space>
	);
};
export default JdCloudWarehouse;
